﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using GestionPedago.EntityContext;

namespace Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'Entity Titre
    /// </summary>
    public abstract class TitreOPManager
    {
        /// <summary>
        /// Méthode de récupération de la liste des titres
        /// </summary>
        /// <param name="orderByLibelleLongAsc">Tri par libellé long (asc = true)</param>
        /// <returns></returns>
        public static List<Titre> GetListeTitre(bool orderByLibelleLongAsc = true)
        {
            try
            {
                IQueryable<Titre> query = EntityConnection.GetContext().Titre;
                if (orderByLibelleLongAsc)
                {
                    query = query.OrderBy(it => it.LibelleLong);
                }
                else
                {
                    query = query.OrderByDescending(it => it.LibelleLong);
                }
                query.Select(it => it);

                List<Titre> lst = query.ToList();

                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Méthode de récupération d'un titre par son identifiant
        /// </summary>
        /// <param name="code">Identifiant du titre</param>
        /// <returns>Le titre</returns>
        public static Titre GetTitreByCode(string code)
        {
            try
            {
                var query = from it in EntityConnection.GetContext().Titre
                            where it.CodeTitre == code
                            select it;

                Titre titre = null;

                titre = query.First();

                return titre;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
